package com.laoli.controller;

import com.laoli.entity.PayOrder;
import com.laoli.entity.PayOrderRecord;
import com.laoli.entity.UserCourseOrder;
import com.laoli.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: lagou-edu-web
 * @Author: GuoAn.Sun
 * @CreateTime: 2020-09-09 16:11
 * @Description:
 */
@RestController
@RequestMapping("order")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("saveOrder")
    public String saveOrder(String orderNo,String user_id , String course_id,String activity_course_id,String source_type,Double price) {
        //保存支付订单
        orderService.saveOrder(orderNo, user_id, course_id, activity_course_id, source_type);
        //保存支付订单日志
        //保存订单的记录日志
        PayOrderRecord record = new PayOrderRecord();
        record.setOrder_no(orderNo);
        record.setType( "CREATE");
        record.setFrom_status("0");
        record.setTo_status("1");
        record.setPaid_amount(price);
        record.setCreated_by(user_id);
        record.setCreated_at(new Date());

        orderService.saveOrderRecord(record);
        return orderNo;
    }

    @GetMapping("updateOrder")
    public Integer updateOrder(String orderNo,Integer status,String user_id,String course_id,String course_name,double price) {

        Integer integer = orderService.updateOrder(orderNo, status);

        if(integer==1){//支付成功
            //记录支付详情
            PayOrder po = new PayOrder();
            po.setOrder_no (orderNo);
            po.setUser_id(user_id);
            po.setProduct_id(course_id);
            po.setProduct_name(course_name);
            po.setAmount(price);
            po.setCount(1);
            po.setCurrency("cny");
            po.setChannel("weChat");
            po.setStatus(2);
            po.setOrder_type(1);
            po.setSource(3);
            po.setClient_ip("192.168.1.1"); //自行解决:获取客户端ip
            po.setCreated_time (new Date());
            po.setUpdated_time(new Date());
            orderService.saveOrderInfo(po);


            //保存订单的记录日志
            PayOrderRecord record = new PayOrderRecord();
            record.setOrder_no(orderNo);
            record.setType( "PAY");
            record.setFrom_status("1");
            record.setTo_status("2");
            record.setPaid_amount(price);
            record.setCreated_by(user_id);
            record.setCreated_at(new Date());
            System.out.println("创建订单记录="+orderNo);
            orderService.saveOrderRecord(record);
        }
        return integer;
    }

    @GetMapping("deleteOrder")
    public Integer deleteOrder(String orderno ) {
        Integer integer = orderService.deleteOrder(orderno);
        return integer;
    }


    @GetMapping("getOkOrderCouesrIds")
    public List<Object> getOkOrderCouesrIds(@RequestParam Integer userid){
        List<UserCourseOrder> list1 = orderService.getOkOrderCouesrIds(userid);
        List<Object> ids=new ArrayList<>();
        for (UserCourseOrder id : list1) {
          ids.add(id.getCourseId());
        }
        return ids;
    }
}
